package com.flame.shapeville.component.task1.shape3D;

import javafx.scene.paint.Color;
import javafx.scene.shape.MeshView;
import javafx.scene.shape.ObservableFaceArray;
import javafx.scene.shape.TriangleMesh;

import java.util.ArrayList;

/**
 * Represents a cone used in Task 1.
 * <p>
 * This class constructs a cone using a TriangleMesh with custom vertex data.
 *
 * @author Flame Dev Team
 * @version 1.0
 * @since 2025-05-12
 */
public class JfCone extends JfShape3D {

    /**
     * Constructs a new cone with the specified size.
     *
     * @param size the bounding box size for the cone
     */
    public JfCone(double size) {
        super(size);
        TriangleMesh mesh = new TriangleMesh();
        float centerX = (float) size / 2, centerZ = -(float) size / 2, topY = 0f, bottomY = (float) size;
        float radio = (float) size / 2;
        mesh.getPoints().addAll(
                centerX, bottomY, centerZ,
                centerX, topY, centerZ
        );
        for (int i = 1; i <= 360; i++) {
            double angle = Math.toRadians(i);
            double x = centerX + radio * Math.cos(angle);
            double z = centerZ + radio * Math.sin(angle);
            mesh.getPoints().addAll((float) x, bottomY, (float) z);
        }
        mesh.getTexCoords().addAll(0.0f, 0.0f);

        ArrayList<Integer> elements = getIntegers();
        int[] faceArray = elements.stream().mapToInt(Integer::intValue).toArray();
        mesh.getFaces().addAll(faceArray);

        shape = new MeshView(mesh);
        initBaseParams(Color.CYAN);
    }

    /**
     * Generates face indices for the cone's mesh.
     *
     * @return list of integers representing triangle faces
     */
    private static ArrayList<Integer> getIntegers() {
        ArrayList<Integer> elements = new ArrayList<>();
        for (int i = 1; i < 360; i++) {
            elements.add(0);
            elements.add(0);
            elements.add(i + 2);
            elements.add(0);
            elements.add(i + 1);
            elements.add(0);

            elements.add(1);
            elements.add(0);
            elements.add(i + 1);
            elements.add(0);
            elements.add(i + 2);
            elements.add(0);
        }
        elements.add(0);
        elements.add(0);
        elements.add(2);
        elements.add(0);
        elements.add(361);
        elements.add(0);
        elements.add(1);
        elements.add(0);
        elements.add(361);
        elements.add(0);
        elements.add(2);
        elements.add(0);
        return elements;
    }
}
